/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wli.web.java.db;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author wli
 */
@ManagedBean
@SessionScoped
public class WordController implements Serializable {

    private DataModel wordList;
    private WordHelper wdHelper;
    private Word current;
    private int selected_word_id;

    /** Creates a new instance of WordController */
    public WordController() {
        wdHelper = new WordHelper();
    }

    public WordController(int wordId) {
        wdHelper = new WordHelper();
        selected_word_id = wordId;
    }

    public DataModel getWordList() {
        if (wordList == null) {
            wordList = new ListDataModel(wdHelper.getWordList());
        }
        return wordList;
    }

    public Word getSelected() {

        //System.out.println(">>>>>>>>>>>>>>>>>>>>WordController:getSelected>>>>>>>AAAAAAAAAAAAAAAAA========="+selected_word_id);
        if (current == null) {
            current = wdHelper.getWordById(selected_word_id);
        }
        //System.out.println(">>>>>>>>>>>>>>>>>>>>>WordController:getSelected>>>>>>BBBBBBBBBBBBBBBBB========"+selected_word_id);
        selected_word_id = current.getWordId();

        return current;
    }

    public String prepareView() {
        if (current != null) {
            //System.out.println("====================VocabularyController:prepareView========AAAAAAAAAAAAAAAAAAA========" + current.getWord());
            //System.out.println("====================VocabularyController:prepareView========AAAAAAAAAAAAAAAAAAA========" + current.getDefinition());
        }
        current = (Word) getWordList().getRowData();
        System.out.println("====================VocabularyController:prepareView========BBBBBBBBBBBBBBBB========" + current.getWord());
        //System.out.println("====================VocabularyController:prepareView========BBBBBBBBBBBBBBBB========" + current.getDefinition());
        return "index3";
    }
}
